﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LiteQuery.Controls
{
    public partial class FileRowPanel : UserControl
    {
        public string ID {
            get { return Guid.NewGuid().ToString(); }
        }

        public event EventHandler OnRemoved;

        private FileInfo _fileInfo;
        public FileInfo FileInfo
        {
            get {
                return _fileInfo;
            }
            set {
                _fileInfo = value;
                if(_fileInfo!=null)
                    txtFileName.Text = _fileInfo.Name.Substring(0, _fileInfo.Name.Length - _fileInfo.Extension.Length);
            }
        }

        public string FileName {
            get {
                return txtFileName.Text;
            }
        }

        public string FullFileName
        {
            get
            {
                if (_fileInfo != null)
                    return _fileInfo.FullName;
                else
                    return "";
            }
        }

        public string TableName
        {
            get
            {
                return txtTableName.Text;
            }
            set {
                txtTableName.Text = value;
            }
        }

        public int TypeValue
        {
            get
            {
                return cboType.SelectedIndex;
            }
        }

        public FileRowPanel()
        {
            InitializeComponent();
            this.cboType.SelectedIndex = 0;
            this.cboType.SelectedIndexChanged += CboType_SelectedIndexChanged;
        }


        private void CboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (var item in this.Parent.Controls)
            {
                var p = item as FileRowPanel;
                if (p is null)
                    continue;
                if (p.ID == this.ID)
                    continue;
                if (p.TypeValue != this.TypeValue)
                    continue;
                // 0 默认不分类
                if (p.TypeValue == 0)
                    continue;
                this.TableName= p.TableName;
            }
        }

        public FileRowPanel(FileInfo fi)
        {
            InitializeComponent();
            _fileInfo = fi;
            txtFileName.Text = _fileInfo.Name.Substring(0, _fileInfo.Name.Length- _fileInfo.Extension.Length);
            txtTableName.Text = _fileInfo.Name.Substring(0, _fileInfo.Name.Length - _fileInfo.Extension.Length);

            this.cboType.SelectedIndex = 0;
            this.cboType.SelectedIndexChanged += CboType_SelectedIndexChanged;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            this.Parent.Controls.Remove(this);
            if (OnRemoved != null)
                OnRemoved(this, null);
        }
    }
}
